import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';

import '../../../data/db/models/bt_order.dart';
import '../../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../../widgets/pagination/bt_no_more_items_indicator.dart';
import '../../utils/bt_app_bar_utils.dart';
import '../models/bt_my_ticket_model.dart';
import '../widgets/bt_order_item_widget.dart';

@RoutePage(name: "myTicketRoute")
class BtMyTicketPage extends StatelessWidget {
  const BtMyTicketPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          width: double.infinity,
          height: double.infinity,
          'assets/images/bt_my_ticket_full_bg.png',
          fit: BoxFit.fill,
        ),
        appBar(context, 'My Ticket', null),
        Container(
          margin: const EdgeInsets.only(top: 105),
          padding: const EdgeInsets.only(left: 20, right: 20),
          child: ChangeNotifierProvider(
            create: (context) => BtMyTicketModel(),
            child: Consumer<BtMyTicketModel>(builder: (context, model, _) {
              return RefreshIndicator(
                onRefresh: () => Future.sync(
                  () => model.pagingController.refresh(),
                ),
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: PagedListView<int, BtOrder>(
                    pagingController: model.pagingController,
                    builderDelegate: PagedChildBuilderDelegate<BtOrder>(
                      itemBuilder: (context, item, index) {
                        return Column(
                          children: [
                            const Divider(
                              height: 15,
                              thickness: 1,
                              color: Colors.transparent,
                            ),
                            BtOrderItemWidget(index: index, item: item, model: model)
                          ],
                        );
                      },
                      firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                        error: model.pagingController.error,
                        onTryAgain: () => model.pagingController.refresh(),
                      ),
                      newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                        error: model.pagingController.error,
                        onTap: () => model.pagingController.retryLastFailedRequest(),
                      ),
                      firstPageProgressIndicatorBuilder: (_) => const BtFirstPageProgressIndicator(),
                      newPageProgressIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNewPageProgressIndicator(),
                      ),
                      noItemsFoundIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNoItemsFoundIndicator(),
                      ),
                      noMoreItemsIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNoMoreItemsIndicator(),
                      ),
                    ),
                  ),
                ),
              );
            }),
          ),
        ),
      ],
    );
  }
}
